#pragma once
#include<memory>
#include <uvw/uvw.hpp>
#include "myrpc/myrpc.pb.h"

namespace myrpc
{
	namespace mynet
	{	
		class Session;
		struct  Package {
			myrpc::Request body;
			std::shared_ptr<Session> tcphandlePtr;
		};

		typedef std::shared_ptr<Package>  PackagePtr;
		class Session : public std::enable_shared_from_this<Session>
		{
		public:
			static std::shared_ptr<Session> Create(const std::shared_ptr<uvw::Loop>& loop)
			{
				auto handle = loop->resource<uvw::TcpHandle>();
				std::shared_ptr<Session> Ptr(new  Session(handle));
				return Ptr;
			}
		public:

			Session() { bodylenght_ = 0; };
			Session(std::shared_ptr<uvw::TcpHandle> handle) :handle_(handle) { bodylenght_ = 0; };
			~Session() = default;

		public:
			void Start();
			int  Send(std::string data, int len);

			void SetOnRead(std::function<void(PackagePtr)>  f)
			{
				packgereachhandle_ = f;
			}
			std::shared_ptr<uvw::TcpHandle> GetHandler()
			{
				return handle_;
			}



		private:
			std::function<void(std::shared_ptr<uvw::TcpHandle>)>  connecthandle_;
			std::function<void(std::shared_ptr<uvw::TcpHandle>)>  closehandle_;
			std::function<void(std::shared_ptr<uvw::TcpHandle>)>  errorhandle_;
			std::function<void(std::shared_ptr<uvw::TcpHandle>)>  writehandle_;
			std::function<void(PackagePtr)>  packgereachhandle_;


			std::shared_ptr<uvw::TcpHandle> handle_;
			int bodylenght_;
			std::string in_;
			std::string out_;

		};
	}
}





